﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;



namespace MVCnExt.Web.UI.Ext
{
    public abstract class ExtObservable : ExtControl
    {
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtScript script { get; set; }

     
        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.util.Observable", this.ClientID, this.ID));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];
            StringBuilder config = new StringBuilder("{");

            if (script != null && !String.IsNullOrEmpty(script.text))
            {
#if DEBUG
                config.AppendLine();
#endif
                config.Append(script.text);
#if DEBUG
                config.AppendLine();
#endif
                config.Append("}");
                desc.AddPropertyBase("listeners", config);
            }

            RegisterClientEvents(desc);

            return list;
        }
    }

    public abstract class ExtConfigObservable : ExtConfig
    {
        /// <summary>
        /// An array of field definition objects, or field name strings.
        /// </summary>                      
        [Description("An array of field definition objects, or field name strings.")]
        [Bindable(true)]
        [DefaultValue("")]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtClientScript listeners { get; set; }

        /// <summary>
        /// A custom validation function to be called during field validation (defaults to null). 
        /// If available, this function will be called only after the basic validators all return true, 
        /// and will be passed the current field value and expected to return boolean true 
        /// if the value is valid or a string error message if invalid.
        /// </summary>
        [Description("A custom validation function to be called during field validation (defaults to null). If available, this function will be called only after the basic validators all return true, and will be passed the current field value and expected to return boolean true if the value is valid or a string error message if invalid.")]
        [Bindable(true)]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtClientScript validator { get; set; }

        [Bindable(true)]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtClientScript handler { get; set; }

        [Bindable(true)]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ExtClientScript fn { get; set; }


        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();
            if (list.Count == 0 && !String.IsNullOrEmpty(this.id))
                list.Add(new ExtScriptControlDescriptor("Ext.util.Observable", this.id, this.id));
            else if (list.Count == 0)
                list.Add(new ExtScriptControlDescriptor("Ext.util.Observable", "ctl00"));

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            StringBuilder config;

            //add listeners
            if (listeners != null && !String.IsNullOrEmpty(listeners.script.text))
            {
                config = new StringBuilder("{");
#if DEBUG
                config.AppendLine();
#endif
                config.Append(listeners.script.text);
#if DEBUG
                config.AppendLine();
#endif
                config.Append("}");

                desc.AddPropertyBase("listeners", config);
            }

            //add validator
            if (validator != null && !String.IsNullOrEmpty(validator.script.text))
            {
                config = new StringBuilder();
#if DEBUG
                config.AppendLine();
#endif
                config.Append(validator.script.text);
#if DEBUG
                config.AppendLine();
#endif

                desc.AddPropertyBase("validator", config);
            }

            //add handler
            if (handler != null && !String.IsNullOrEmpty(handler.script.text))
            {
                config = new StringBuilder();
#if DEBUG
                config.AppendLine();
#endif
                config.Append(handler.script.text);
#if DEBUG
                config.AppendLine();
#endif

                desc.AddPropertyBase("handler", config);
            }

            //add fn
            if (fn != null && !String.IsNullOrEmpty(fn.script.text))
            {
                config = new StringBuilder();
#if DEBUG
                config.AppendLine();
#endif
                config.Append(fn.script.text);
#if DEBUG
                config.AppendLine();
#endif

                desc.AddPropertyBase("fn", config);
            }

            return list;
        }
    }
}
